package cn.client.ui.view.main.user.emotionLog

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.ui.components.BaseTopBar
import cn.client.ui.view.main.mindfulness.HomeSection
import kotlinx.coroutines.launch

// 情绪日志信息页面
@Composable
fun EmotionLogPage(
    id: Int,
    navController: NavController,
    viewModel: EmotionLogViewModel = hiltViewModel()
){
    val emotionLogInfoState = viewModel.emotionLogInfo.collectAsState(initial = null)
    val emotionLogInfo = emotionLogInfoState.value

    val context = LocalContext.current
    val coroutineScope = rememberCoroutineScope()

    coroutineScope.launch {
        viewModel.dispatch(EmotionLogViewModel.PageIntent.SelectInfo(id))
    }

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when (it) {
                is EmotionLogViewModel.PageEvent.Error -> {
                    Toast.makeText(context, "提示：${it.error}", Toast.LENGTH_SHORT).show()
                }
                else -> Unit
            }

        }
    }
    Scaffold(
        topBar = {
            BaseTopBar(
                title = "情绪日志",
                onClickArrowBack = {
                    coroutineScope.launch {
                        navController.navigateUp()
                    }
                }
            )
        }
    ){
        Column(
            modifier = Modifier.padding(it)
        ) {
            if (emotionLogInfo != null) {
                HomeSection(title = "事件") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        Text(text = emotionLogInfo.incident)
                    }
                }
                HomeSection(title = "感受") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        Text(text = emotionLogInfo.feel)
                    }
                }
                HomeSection(title = "想法") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        Text(text = emotionLogInfo.idea)
                    }
                }
                HomeSection(title = "行动") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        Text(text = emotionLogInfo.act)
                    }
                }
                HomeSection(title = "结果") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        Text(text = emotionLogInfo.ending)
                    }
                }
            }
        }
    }
}